package com.moke.util.mouse
{
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.ui.Mouse;

	public class EffectMouse extends Sprite
	{
		static private var instance:EffectMouse;
		
		static public function getInstance(stage:Stage = null):EffectMouse
		{
			if (instance != null){ 
				return instance;
			}
			
			if (null == stage){
				return null;
			}
			
			instance = new EffectMouse(stage);
			return instance;
		}
		
		static public function startEffect():void{
			instance.startEffect() ;
		}
		
		static public function endEffect():void{
			instance.endEffect() ;
		}
		
		private var _stage:Stage ;
		private var x0:Number = mouseX;
		private var y0:Number = mouseY;
		
		public function EffectMouse(stage:Stage)
		{
			_stage = stage ;
		}
		
		public function startEffect():void
		{			
			stage.addEventListener(Event.ENTER_FRAME, mouseMoveHandler);
			stage.addEventListener(MouseEvent.MOUSE_OUT, mouseOutHandler);
			stage.addEventListener(MouseEvent.MOUSE_OVER, mouseOverHandler);
			Mouse.hide();
		}
		
		public function endEffect():void{
			
			stage.removeEventListener(Event.ENTER_FRAME, mouseMoveHandler);
			stage.removeEventListener(MouseEvent.MOUSE_OUT, mouseOutHandler);
			stage.removeEventListener(MouseEvent.MOUSE_OVER, mouseOverHandler);
			Mouse.show() ;
		}
				
		private function mouseOverHandler(e:MouseEvent):void 
		{
			EffectMouseBall.show = true;
		}
		
		private function mouseOutHandler(e:MouseEvent):void 
		{
			EffectMouseBall.show = false;
		}
		
		private function mouseMoveHandler(e:Event):void 
		{
			var x1:Number = mouseX;
			var y1:Number = mouseY;
			if (x1 != x0 || y1 != y0)
			{
				x0 = x1;
				y0 = y1;
				for (var i:int = 0; i < 2; i++)
				{
					var ball:EffectMouseBall = new EffectMouseBall();
					ball.x = mouseX;
					ball.y = mouseY;
					addChild(ball);
				}
			}
			
		}
		
	}
}